import { Component } from "react";
import ThemeContext from "./context";

export default class Votemain extends Component{
    //后代组件：用contextType(必须这个名称)，来接收，执行上下文
    static contextType=ThemeContext;
    render(){
        //参数（所有参数），就在this.context里面
        console.log(this.context);
        let {supNum,oppNum} = this.context;
        return <div>
            <h2>支持人数：{supNum}</h2>
            <h2>反对人数：{oppNum}</h2>
            <h2>支持率：{this.computed()}</h2>
        </div>
    }
    computed=()=>{
      let {supNum,oppNum}=this.context;
      let total=supNum+oppNum;
      if(total<=0){
         return "0.00%";
      }
      return (supNum/total*100).toFixed(2)+"%";
    }
}